import { jsonToUrlParam, getApiUrl } from '../../urlSearchParams';
import type { GetIdentityRolesInput, IdentityRoleDto, RoleCreateDto, RoleUpdateDto } from '../../api/role/type';
import type { PagedResultDto } from '../../api/type';

import { authorizationHttpRequest as request } from './authorizationHttpRequest';

// Api地址
enum Api {
    DEFAULT_URL = 'api/basics/roles',
    ROLE_ALL_URL = 'api/basics/roles/all',
    ROLE_PERMISSION_URL = 'api/basics/roles/permissions'
}

// 角色接口
export class RoleHttpRequest {
    // 列表接口
    public getListAsync = (input: GetIdentityRolesInput) => {
        const paramenter = jsonToUrlParam(input);
        const url = getApiUrl(Api.DEFAULT_URL, paramenter);
        return request.get<PagedResultDto<IdentityRoleDto>>(url);
    };

    // 角色权限
    public getPermissionListAsync = (id: string) =>
        request.get<Array<string>>(`${Api.ROLE_PERMISSION_URL}/${id}`);

    // 详情接口
    public getAsync = (id: string) => request.get<IdentityRoleDto>(`${Api.DEFAULT_URL}/${id}`);

    // 创建
    public createAsync = (input: RoleCreateDto) =>
        request.post<IdentityRoleDto>(Api.DEFAULT_URL, input);

    // 更新
    public updateAsync = (id: string, input: RoleUpdateDto) =>
        request.put<IdentityRoleDto>(`${Api.DEFAULT_URL}/${id}`, input);

    // 删除
    public deleteAsync = (id: string) => request.delete<void>(`${Api.DEFAULT_URL}/${id}`);

    // 全部角色
    public getAllAsync = () => request.get<string[]>(Api.ROLE_ALL_URL);
}
